package com.mfh.auto.create.mvvm.autocreatemvvm

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.command.CommandProcessor
import com.intellij.openapi.project.Project

fun String.camelCaseToUnderscore(): String {
    val builder = StringBuilder()
    for (i in this.indices) {
        val char = this[i]
        if (i > 0 && char.isUpperCase()) {
            builder.append('_')
        }
        builder.append(char.lowercaseChar())
    }
    return builder.toString()
}


/**
 * do the action that could be roll-back
 */
fun executeCouldRollBackAction(project: Project?, action: (Project?) -> Unit) {
    CommandProcessor.getInstance().executeCommand(project, {
        ApplicationManager.getApplication().runWriteAction {
            action.invoke(project)
        }
    }, "CreateFile", "AutoMVVM_V2")
}